package com.sh.suhe.utildemos.views.activity.photo.util;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.sh.suhe.utildemos.model.PaPhotoModel;
import com.sh.suhe.utildemos.model.PaPhotoTypeModel;
import com.sh.suhe.utildemos.utils.JsonUtil;
import com.sh.suhe.utildemos.utils.http.NetUtil;
import com.sh.suhe.utildemos.views.activity.photo.PaPhotoActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SoGouUtils {
    private static String TAG= SoGouUtils.class.getSimpleName();
     static  String url = "https://pic.sogou.com/napi/pc/searchList?mode=13&dm=4&cwidth=484&cheight=842&start=0&xml_len=48&query=";
    //https://pic.sogou.com/napi/pc/searchList?mode=13&dm=4&cwidth=484&cheight=842&tagQSign=动漫,0ddc1f81&start=0&xml_len=48&query=壁纸

    public static void url(String query, String tag, PaPhotoActivity.LoadPhoto loadPhoto) {

        String tempUrl  = url + query;
        if (!TextUtils.isEmpty(tag)) {
            tempUrl = tempUrl + "&tagQSign=" + tag;
        }
        Log.e(TAG, tempUrl);
        NetUtil.request(NetUtil.RequestMethod.GET, tempUrl, new HashMap<>(), new NetUtil.RequestCallBack() {
            @Override
            public void onSuccess(int statusCode, String json) {

                JsonObject root = JsonUtil.json2Object(json, JsonObject.class);
                JsonObject data = root.getAsJsonObject("data");
                JsonArray tagList = data.getAsJsonArray("tagList");
                JsonArray items = data.getAsJsonArray("items");
                List<PaPhotoTypeModel> types = new ArrayList<>();
                List<PaPhotoModel> imgList = new ArrayList<>();
                Log.e(TAG, JsonUtil.object2Json(items));
                for (JsonElement type : tagList) {
                    PaPhotoTypeModel model = new PaPhotoTypeModel();
                    JsonObject typeObject = type.getAsJsonObject();
                    model.setType(typeObject.get("value").getAsString());
                    model.setUrl(typeObject.get("key").getAsString());
                    types.add(model);
                }
                for (JsonElement item : items) {
                    PaPhotoModel model = new PaPhotoModel();
                    JsonObject itemObject = item.getAsJsonObject();
                    model.setImg( itemObject.get("thumbUrl").getAsString());
                    model.setDetailUrl(itemObject.get("picUrl").getAsString());
                    imgList.add(model);
                }
                Log.e(TAG, JsonUtil.object2Json(imgList));
                loadPhoto.loadBack(imgList, types, "");
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {

            }
        });
    }

}
